package com.github.xuemingyu.springboot.acticitycloud.service.impl;

import com.github.xuemingyu.springboot.acticitycloud.dao.UserInfoDao;
import com.github.xuemingyu.springboot.acticitycloud.entity.UserInfo;
import com.github.xuemingyu.springboot.acticitycloud.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/2/12
 * \* Time: 17:23
 * \
 */
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfo> implements UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserInfo findByUserId(String userId) {
        UserInfo userInfo = userInfoDao.findFirstByUserId(userId);
        return userInfo;
    }

    public void checkUserInfo(UserInfo userInfo) {
        checkString(userInfo.getName(), "姓名不能为空");
        checkString(userInfo.getUserId(), "userId不能为空");
        checkString(userInfo.getPhone(), "手机号码不能为空");
        checkString(userInfo.getCertificateCode(), "证件号码不能为空");
        checkString(userInfo.getCertificateType(), "证件类型不能为空");
    }

    @Override
    public void insert(UserInfo userInfo) {
        checkUserInfo(userInfo);
        userInfoDao.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        checkUserInfo(userInfo);
        UserInfo one = userInfoDao.findOne(userInfo.getId());
        one.setCertificateCode(userInfo.getCertificateCode());
        one.setCertificateType(userInfo.getCertificateType());
        one.setName(userInfo.getName());
        one.setPhone(userInfo.getPhone());
        one.setUserId(userInfo.getUserId());
        userInfoDao.save(one);
    }

    @Override
    protected JpaRepository<UserInfo, Long> getDao() {
        return userInfoDao;
    }
}
